/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.tasklists;

import com.google.common.collect.Lists;
import com.google.inject.Singleton;
import com.google.inject.Inject;
import listo.client.model.Tasks;
import listo.client.taskfilters.TaskListFilter;
import listo.client.AppConfig;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@Singleton
public class TaskListManager implements ListSelectionListener {

    public interface ActiveTaskListListener {
        public void activeTaskListChanged(TaskList activeTaskList, TaskListFilter.Settings activeFilterSettings);
    }

    public interface TaskSelectionListener {
        public void taskSelectionChanged(TaskList taskList, Tasks selectedTasks);
    }

    private final List<MainFormTaskList> taskLists = new ArrayList<MainFormTaskList>(4);

    private final TaskListFilter.Settings[] filters = new TaskListFilter.Settings[]
            {
                    new TaskListFilter.Settings(null, null),     // everything
                    new TaskListFilter.Settings(null, 0),        // everything up until today
                    new TaskListFilter.Settings(1, 1),           // only tomorrow
                    new TaskListFilter.Settings(null, 6)         // everything up until six days from now
            };

    private final List<ActiveTaskListListener> activeFilterListeners = Lists.newArrayList();
    private final List<TaskSelectionListener> taskSelectionListeners = Lists.newArrayList();

    private MainFormTaskList activeTaskList;
    private Timer timer;

    @Inject
    public TaskListManager(AppConfig appConfig) {
        // we want to repaint the active tasklist regularly so as to update reminder times
        timer = new Timer(appConfig.getTasklistRefresh() * 1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                activeTaskList.repaint();
            }
        });
        timer.start();
    }

    public MainFormTaskList getActiveTaskList() {
        return activeTaskList;
    }

    // called when the tab pane has changed
    public void setActiveTabIndex(int selectedIndex) {
        activeTaskList = taskLists.get(selectedIndex);
        for (ActiveTaskListListener listener : activeFilterListeners) {
            listener.activeTaskListChanged(activeTaskList, filters[selectedIndex]);
        }
        timer.restart();
    }

    public void register(MainFormTaskList list) {
        taskLists.add(list);
        if (activeTaskList == null) activeTaskList = list;
    }

    // called when the task selection within a task list has changed
    public void valueChanged(ListSelectionEvent e) {
        TaskList taskList = (TaskList) e.getSource();
        Tasks selectedTasks = new Tasks(taskList.getSelectedValues());
        for (TaskSelectionListener listener : taskSelectionListeners) {
            listener.taskSelectionChanged(taskList, selectedTasks);
        }
        timer.restart();
    }

    public void addActiveTaskListListener(ActiveTaskListListener listener) {
        activeFilterListeners.add(listener);
    }

    public void addTaskSelectionListener(TaskSelectionListener listener) {
        taskSelectionListeners.add(listener);
    }

}